//
//  RBShopApplyVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/29.
//

#import "RBShopApplyVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBSearchTagView.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "UIViewController+RBAdd.h"
#import "RBPager.h"
#import "RanTool.h"
#import "RBShopApplyPageVC.h"
#import "RBRegularWebView.h"
#import "RBShopApplyStateResponse.h"
#import "RBShopApplyPageStep2VC.h"
#import "RBShopApplyPageStep3VC.h"
#import "RBShopApplyPageStep4VC.h"

@interface RBShopApplyVC ()

@property(nonatomic, strong)UIScrollView *scrollView;

@end

@implementation RBShopApplyVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"商家入驻";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillDataWithHtml: (NSString *)html
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[RanTool imgViewWithMode:0 radius:0 imgName:@"shopApplyTopPic"] withHeight:Zoom(322) width:Zoom(322) top:Zoom(9) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        RBRegularWebView *webView = RBRegularWebView.new;
        [view view_addView:webView withHeight:30 width:CGFLOAT_MIN top:Zoom(28) bottom:Zoom(38) left:Zoom(16) right:Zoom(16) centerX:0];
        [webView setHtmlString:html];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(11) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    
    [self.scrollView addView:({
        UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:0 color:UIColor.whiteColor align:1 text:@"立即申请" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(23)];
        [view rbAddTapGesWithTarget:self action:@selector(applyBtnTapped)];
        view;
    }) withHeight:Zoom(46) width:Zoom(349) top:Zoom(39) bottom:MAX(kRBSafeBottomHeight, Zoom(40)) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"han_article_ruzhu" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(RBResponse *data) {
        NSDictionary *dic = data.responseObject;
        [weak_self fillDataWithHtml:dic[@"data"]];
    } failBlock:nil reloadBlock:^{
        [weak_self getData];
    }];
}

#pragma mark - Event

- (void)applyBtnTapped
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"han_step_check" post:YES log:YES responseClass:RBShopApplyStateResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBShopApplyStateResponse *data) {
        if(data.data.step && data.data.status == 0)
        {
            [weak_self.view showTip:@"信息审核中,请耐心等候" completion:nil];
            return;
        }
        if(data.data.step == 4 && data.data.status == 1)
        {
            [weak_self.view showTip:@"您的入驻审核已通过" completion:nil];
            return;
        }
        
        if(data.data.step <= 1)
        {
            RBShopApplyPageVC *vc = RBShopApplyPageVC.new;
            vc.response = data;
            [weak_self.navigationController pushViewController:vc animated:YES];
        }
        else if(data.data.step == 2)
        {
            RBShopApplyPageStep2VC *vc = RBShopApplyPageStep2VC.new;
            vc.response = data;
            [weak_self.navigationController pushViewController:vc animated:YES];
        }
        else if(data.data.step == 3)
        {
            RBShopApplyPageStep3VC *vc = RBShopApplyPageStep3VC.new;
            vc.response = data;
            [weak_self.navigationController pushViewController:vc animated:YES];
        }
        else if(data.data.step == 4)
        {
            RBShopApplyPageStep4VC *vc = RBShopApplyPageStep4VC.new;
            vc.response = data;
            [weak_self.navigationController pushViewController:vc animated:YES];
        }
    } failBlock:^(RBShopApplyStateResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

@end
